package com.haixee.parkdemo.auth.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author GUIFENG GUO
 * 2020/3/22 23:45
 */
public class PasswordUtil {

    private final static BCryptPasswordEncoder ENCODER = new BCryptPasswordEncoder();

    public static String encode(String password) {
        return ENCODER.encode(password);
    }

    public static boolean validate(String password, String dbPw) {
        return ENCODER.matches(password, dbPw);
    }

    public static int checkLevel(String password) {

        int modes = 0;

        for (char c : password.toCharArray()) {
            if (c >= 48 && c <= 57) {
                //数字
                modes |= 1;
            } else if (c >= 65 && c <= 90) {
                //大写字母
                modes |= 2;
            } else if ((c >= 97 && c <= 122)) {
                //小写字母
                modes |= 4;
            } else {
                modes |= 8; //特殊字符
            }
        }

        return (modes & 1) + ((modes >> 1) & 1) + ((modes >> 2) & 1) + ((modes >> 3) & 1);
    }

    public static void main(String[] args) {
        System.out.println(PasswordUtil.encode("1"));
    }
}
